package com.ueffort.social.platform;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import androidx.core.content.FileProvider;

import com.taptap.sdk.share.TapTapShareBuilder;
import com.taptap.sdk.share.TapTapShareResult;
import com.ueffort.social.SocialProvider;
import com.ueffort.social.SocialSDK;
import com.ueffort.social.action.AuthAction;
import com.ueffort.social.action.RealAction;
import com.ueffort.social.action.ShareAction;
import com.ueffort.social.action.LeaderboardAction;
import com.ueffort.social.action.AchievementAction;
import com.ueffort.social.action.SocialAction;
import com.ueffort.social.inter.ShareObject;
import com.ueffort.social.utils.Apk;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TapcnSocialProvider implements SocialProvider{
    private static TapcnSocialProvider instance = new TapcnSocialProvider();

    private Activity activity;

    private AuthAction authAction;
    private ShareAction shareAction;

    public static TapcnSocialProvider Instance(){
        return instance;
    }

    @Override
    public boolean isInstall() {
        return true;
    }

    @Override
    public boolean isShared() {
        return true;
    }

    @Override
    public boolean isLeaderboard() {
        return false;
    }

    @Override
    public boolean isAchievement() {
        return false;
    }

    @Override
    public boolean isReal() {
        return true;
    }

    @Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        return true;
    }

    @Override
    public void onCreate(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onDestroy(Activity activity) {

    }

    @Override
    public void Init(String appId, Activity activity, SocialAction action) {

    }

    @Override
    public void Authorize(AuthAction action) {
    }

    @Override
    public void AuthorizeStatus(AuthAction action) {
    }

    @Override
    public void UpdateData(Map<String, String> info) {

    }

    @Override
    public void Real(RealAction action) {

    }

    @Override
    public void Logout(){

    }

    @Override
    public void ShareWithObject(ShareObject share, ShareAction action) {
        // android.media.MediaScannerConnection.scanFile(activity, new String[]{share.thumb}, new String[] {"image/png"}, null);
        File imageFile = new File(share.thumb);

        // 使用 FileProvider 获取 Uri
        Uri imageUri = FileProvider.getUriForFile(activity,
                activity.getApplicationContext().getPackageName() + ".fileProvider",
                imageFile);
        ArrayList al = new ArrayList();
        al.add(imageUri);
        int resultCode = new TapTapShareBuilder()
                .addTitle(share.title) // 分享标题
                .addContents(share.description) // 分享内容
//             .addHashtagIds("hashtag ids") // 话题 Id
//             .addGroupLabelId("group ids") // 论坛标签 Id
                .addFooterImageUrls(al) // 分享的图片
                .build()
                .share(activity);

        switch (resultCode){
            case TapTapShareResult.SUCCESS: // 分享成功
                action.onResult(SocialSDK.SocialPlatformType.TapCn);
                break;
            case TapTapShareResult.ERROR_NOT_INSTALL: // 分享失败：未安装 TapTap 应用
                shareAction.onError(SocialSDK.SocialPlatformType.Wechat, new Throwable("un install") );
                break;
            default: // 分享失败：TapTap 应用不支持
                shareAction.onError(SocialSDK.SocialPlatformType.Wechat, new Throwable("un support") );
                break;
        }
    }

    @Override
    public void SubmitLeaderboard(String key, int value, LeaderboardAction action) {
        action.onCancel(SocialSDK.SocialPlatformType.Facebook);
    }

    @Override
    public void AddAchievement(String key, int value, AchievementAction action) {
        action.onCancel(SocialSDK.SocialPlatformType.Facebook);
    }
}